package com.xxx.video.main.fragments;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.dynamic.activitys.MessageDetailsActivity;
import com.xxx.video.main.adapters.SystemMessageListAdapter;
import com.xxx.video.main.beans.SystemMessageBean;
import com.xxx.video.main.presenters.ISystemMessageView;
import com.xxx.video.main.presenters.SystemMessagePresenter;

import java.util.Objects;

import butterknife.BindView;

public class SystemMessageFragment extends BaseFragment<SystemMessagePresenter> implements ISystemMessageView,
        RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener, BaseRecycleViewAdapter.OnItemClickListener<SystemMessageBean.PageBean.ListBean> {

    @BindView(R.id.rcv_system_message_list)
    RefreshRecyclerView rcvSystemMessageList;
    private int page = 1;
    /**
     * adapter
     */
    private SystemMessageListAdapter mSystemMessageListAdapter;

    public static SystemMessageFragment newInstance(Bundle args) {
        SystemMessageFragment fragment = new SystemMessageFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected SystemMessagePresenter createPresenter() {
        return new SystemMessagePresenter(Objects.requireNonNull(getActivity()), this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_system_message;
    }

    @Override
    public void initData(Bundle extras) {
        showRootLayoutStatus(1);
        initList();
        getPresenter().getMessage(page);
    }

    @Override
    public void refreshData(Bundle extras) {
        page = 1;
        getPresenter().getMessage(page);
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void initList() {
        rcvSystemMessageList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mSystemMessageListAdapter = new SystemMessageListAdapter(Objects.requireNonNull(getActivity()));
        rcvSystemMessageList.setAdapter(mSystemMessageListAdapter);
        rcvSystemMessageList.setOnRecyclerViewRefreshOrLoadMoreListener(this);
        mSystemMessageListAdapter.setOnItemClickListener(this);
    }

    @Override
    public void initData2View(SystemMessageBean successOb) {
        rcvSystemMessageList.resetState();
        if (page == 1) {
            mSystemMessageListAdapter.setData(successOb.page.list);
            if (ListUtils.isEmpty(successOb.page.list)) {
                showLayoutStatus(2);
            }
        } else {
            mSystemMessageListAdapter.addData(successOb.page.list);
        }
        if (ListUtils.isEmpty(successOb.page.list) || successOb.page.list.size() < 20) {
            rcvSystemMessageList.setNoMoreData();
        }
    }

    @Override
    public void onRecyclerviewRefresh() {
        page = 1;
        getPresenter().getMessage(page);
    }

    @Override
    public void onRecyclerviewLoadMore() {
        page++;
        getPresenter().getMessage(page);
    }

    @Override
    public void onItemClick(SystemMessageBean.PageBean.ListBean listBean, int position, int type) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", listBean.fUsermessageId);
        intent2Activity(MessageDetailsActivity.class, bundle);
    }
}
